using Godot;
using System;
using System.Data.Common;

public partial class MainMenu : Control
{
	public MultiplayerScene server;

	Label label_name, label_onlinecount, label_onlinecount_server, label_point;
	LineEdit lineEdit_ip,
	lineEdit_port,
	lineEdit_name,
	lineEdit_globalmsg,
	 lineEdit_msg,
	 lineEdit_globalmsg_server
	 ;
	Button button_connect, button_start, button_match, button_exit;
	RichTextLabel richTextLabel_globalmsg, richTextLabel_msg, richTextLabel_globalmsg_server;
	AnimatedSprite2D animatedSprite2D_state;
	Cell[] cells = new Cell[9];
	Panel Panel_ServerPanel, Panel_ConnectConfig;
	public override void _Ready()
	{
		for (int i = 0; i < 9; i++)
		{
			cells[i] = GetNode<Cell>("Cell" + i);
			cells[i].index = i;
			cells[i].ButtonDown += (Cell cell) =>
			{
				if (!Multiplayer.IsServer())
				{
					if (server != null)
					{
						if (server.player != null)
						{
							if (server.inGame != null)
							{
								if (server.inGame.roundChessman == server.player.chessman)
								{
									server.RpcId(1, "SetCell", server.player.id, cell.index);
								}
							}
						}
					}
				}
			};
		}

		server = GetTree().Root.GetNode<MultiplayerScene>("MultiplayerScene");
		Panel_ServerPanel = GetNode<Panel>("Panel_ServerPanel");
		Panel_ConnectConfig = GetNode<Panel>("Panel_ConnectConfig");
		button_connect = GetNode<Button>("Panel_ConnectConfig/Button_connect");
		button_start = GetNode<Button>("Panel_ConnectConfig/Button_start");
		button_match = GetNode<Button>("Panel/Button_Match");
		button_exit = GetNode<Button>("Panel/Button_Exit");
		lineEdit_ip = GetNode<LineEdit>("Panel_ConnectConfig/LineEdit_Ip");
		lineEdit_port = GetNode<LineEdit>("Panel_ConnectConfig/LineEdit_Port");
		lineEdit_name = GetNode<LineEdit>("LineEdit_name");
		lineEdit_msg = GetNode<LineEdit>("LineEdit_msg");
		lineEdit_globalmsg = GetNode<LineEdit>("LineEdit_globalmsg");
		lineEdit_globalmsg_server = GetNode<LineEdit>("Panel_ServerPanel/LineEdit_globalmsg_server");
		label_point = GetNode<Label>("Label_point");
		label_name = GetNode<Label>("Label_Name");
		label_onlinecount = GetNode<Label>("Label_onlinecount");
		label_onlinecount_server = GetNode<Label>("Panel_ServerPanel/Label_onlinecount_server");
		richTextLabel_globalmsg = GetNode<RichTextLabel>("RichTextLabel_globalmsg");
		richTextLabel_globalmsg_server = GetNode<RichTextLabel>("Panel_ServerPanel/RichTextLabel_globalmsg_server");
		richTextLabel_msg = GetNode<RichTextLabel>("RichTextLabel_msg");
		animatedSprite2D_state = GetNode<AnimatedSprite2D>("Panel/TextureRect/AnimatedSprite2D_State");
		lineEdit_ip.TextChanged += (string str) => { server.ip = str; };
		lineEdit_port.TextChanged += (string str) => { server.port = str.ToInt(); };
		button_start.ButtonDown += () => { server.StartServer(); Panel_ServerPanel.Visible = true; };
		button_connect.ButtonDown += () => { server.ConnectServer(); Panel_ConnectConfig.Visible = false; };

		button_exit.ButtonDown += () =>
		{
			if (server != null && server.player != null)
			{
				server.RpcId(1, "ExitRound", server.player.id);
			}
		};


		button_match.ButtonDown += () =>
		{
			if (server != null && server.player != null)
			{
				server.RpcId(1, "MatchingGame", server.player.id);
				server.machingTime = 0;
			}
		};
		lineEdit_name.TextChanged += (string str) =>
		{
			if (
				!Multiplayer.IsServer() &&
				server.player != null
			)
			{
				server.RpcId(1, "SetPlayerName", server.player.id, str);
			}
		};
		lineEdit_msg.TextSubmitted += (string str) =>
		{
			if (server != null && server.player != null && server.inGame != null)
			{
				server.RpcId(1, "SendRoundMsg", server.player.id, str);
				lineEdit_msg.Text = "";
			}
		};
		lineEdit_globalmsg.TextSubmitted += (string str) =>
		{
			if (Multiplayer.MultiplayerPeer.GetConnectionStatus() == MultiplayerPeer.ConnectionStatus.Connected)
			{
				if (Multiplayer.IsServer())
				{

				}
				else
				{
					server.RpcId(1, "SendGlobalMsg", server.player.id, str);
					lineEdit_globalmsg.Text = "";
				}
			}
		};
		lineEdit_globalmsg_server.TextSubmitted += (string str) =>
		{
			if (server != null && Multiplayer.MultiplayerPeer.GetConnectionStatus() == MultiplayerPeer.ConnectionStatus.Connected)
			{
				if (Multiplayer.IsServer())
				{
					var msg = new Message()
					{
						time = System.DateTime.Now.ToString("HH时 mm分 ss秒"),
						name = "Server",
						msg = str
					};
					server.messages.Add(msg);
					server.Rpc("SetMessage", msg.ToString());
					lineEdit_globalmsg_server.Text = "";
				}
			}
		};
	}

	public override void _Process(double delta)
	{
		if (Multiplayer.MultiplayerPeer.GetConnectionStatus() == MultiplayerPeer.ConnectionStatus.Connected && Multiplayer.IsServer() && server != null)
		{
			Panel_ServerPanel.Visible = true;
			label_onlinecount_server.Text = $"在线:{server.players.Count}";
			if (server.messages.Count > 30) server.cilentMessages.RemoveAt(0);
			string str = "";
			for (int i = 0; i < server.messages.Count; i++)
			{
				str += server.messages[i].ToString();
			}
			richTextLabel_globalmsg_server.Text = str;
		}
		if (Multiplayer.MultiplayerPeer.GetConnectionStatus() == MultiplayerPeer.ConnectionStatus.Connected && server != null && !Multiplayer.IsServer())
		{
			Panel_ConnectConfig.Visible = false;
			Panel_ServerPanel.Visible = false;
			if (server.player != null)
			{
				label_onlinecount.Text = $"在线:{server.onlineCount}";
				if (server.cilentMessages.Count > 30) server.cilentMessages.RemoveAt(0);
				string str = "";
				for (int i = 0; i < server.cilentMessages.Count; i++)
				{
					str += server.cilentMessages[i];
				}
				richTextLabel_globalmsg.Text = str;
				if (server.machingTime >= 0f)
				{
					button_match.Text = $"匹配中..({((int)server.machingTime)}秒)";
				}
				else
				{
					button_match.Text = "匹配";
				}
			}
			if (server.inGame != null)
			{
				string str = "";
				for (int i = 0; i < server.inGame.messages.Count; i++)
				{
					str += server.inGame.messages[i];
				}
				richTextLabel_msg.Text = str;


				animatedSprite2D_state.Visible = true;
				button_match.Visible = false;
				button_exit.Visible = true;
				animatedSprite2D_state.Animation = $"{server.player.chessman}";
				if (server.inGame.playerA.id == server.player.id)
				{

					label_name.Text = server.inGame.playerB.name;
					label_point.Text = $"{server.inGame.playerA.point}:{server.inGame.playerB.point}";
				}
				if (server.inGame.playerB.id == server.player.id)
				{
					label_name.Text = server.inGame.playerA.name;
					label_point.Text = $"{server.inGame.playerB.point}:{server.inGame.playerA.point}";
				}

				for (int x = 0; x < 3; x++)
				{
					for (int y = 0; y < 3; y++)
					{
						cells[x * 3 + y].state = server.inGame.guid[x, y];
					}
				}
			}
			else
			{
				button_match.Visible = true;
				button_exit.Visible = false;
				label_name.Text = "";
				animatedSprite2D_state.Visible = false;
			}
		}
		else
		{
			button_match.Visible = true;
			button_exit.Visible = false;
			label_name.Text = "";
			animatedSprite2D_state.Visible = false;
		}
	}
}
